<?php
	class QuestionManager{
		  private $_db;
	
		  public function __construct($db){
				$this->setDb($db);
		  }
		  
		  public function setDb(PDO $db){
				$this->_db = $db;
		  }
		  
		  public function add(Question $question){// permet d'ajouter une question
				$q = $this->_db->prepare('INSERT INTO question SET contenu = :contenu, idRefExercice = :idRefExercice, image = :image, ordre = :ordre');
				$q->bindValue(':contenu', $question->contenu());
				$q->bindValue(':idRefExercice', $question->idRefExercice());
				$q->bindValue(':image', $question->image());
				
				$ordre = $this->getNbQuestion($question->idRefExercice());
				$ordre++;
				$q->bindValue(':ordre', $ordre);
				$q->execute();
		  }
		  
		  public function delete(Question $question){// permet de supprimer une question
			  $this->_db->exec('DELETE FROM question WHERE id = '.$question->id());
		  }
		  
		  public function update(Question $question){// permet de modifier une question
				$q = $this->_db->prepare('UPDATE question SET contenu = :contenu, idRefExercice = :idRefExercice, image = :image, ordre = :ordre WHERE id ='.$question->id());
				$q->bindValue(':contenu', $question->contenu());
				$q->bindValue(':idRefExercice', $question->idRefExercice());
				$q->bindValue(':image', $question->image());
				$q->bindValue(':ordre', $question->ordre());
				$q->execute();
		  }
		  
		  public function get($id){// retourne l'objet question d'id correspondant
				$id = (int) $id;
				$q = $this->_db->query('SELECT * FROM question WHERE id = '.$id);
				$donnees = $q->fetch();
				return new Question($donnees);
		  }
		  
		  public function getByExercice($idExercice){// retourne un tableau des questions correspondant à un exercice.
				$question = array();
				$q = $this->_db->query('SELECT * FROM question WHERE idRefExercice = '.$idExercice);
				while ($donnees = $q->fetch()){
					$question[] = new Question($donnees);
				}
				return $question;
		  }	
		  public function getNbQuestion($idExo){// retourne le nombre de question d'un exercice
				$q = $this->_db->query('SELECT COUNT(*) FROM question WHERE idRefExercice = '.$idExo);
				$donnees = $q->fetch();
				return $donnees[0];				  
		  }	
		  
		  public function getNbReponse($idQuest){ //  retourne le nombre de réponse d'une question
				$q = $this->_db->query('SELECT COUNT(*) FROM reponse, question WHERE reponse.idRefQuestion = question.id AND question.id ='.$idQuest);
				$donnees = $q->fetch();
				return $donnees[0];				  
		  }			  

		public function ordreUp($id) { //Change l'ordre de la question (+1)
			$question = $this->get($id);
			$ordreM = $question->ordre() - 1;
			$q = $this->_db->query('SELECT * FROM question WHERE idRefExercice = '.$question->idRefExercice().' AND ordre = '.$ordreM);
			
			if($q->rowCount() == 1)
			{
				$question2 = new Question($q->fetch());
				$question2->setOrdre($question2->ordre()+1);
				$question->setOrdre($question->ordre()-1);
				$this->update($question);
				$this->update($question2);
			}
		}
		
		public function ordreDown($id) { //Change l'ordre de la question (-1)
			$question = $this->get($id);
			$ordreP = $question->ordre() + 1;
			$q = $this->_db->query('SELECT * FROM question WHERE idRefExercice = '.$question->idRefExercice().' AND ordre = '.$ordreP);
			
			if($q->rowCount() == 1)
			{
				$question2 = new Question($q->fetch());
				$question2->setOrdre($question2->ordre()-1);
				$question->setOrdre($question->ordre()+1);
				$this->update($question);
				$this->update($question2);
			}
		} 
		
		public function ReorganizeOrdre($idExercice) //Reorganise l'ordre des questions après une suppression
		{
			$i=1;
			foreach($this->getByExercice($idExercice) as $question)
			{
				$question->setOrdre($i);
				$this->update($question);
				$i++;
			}
		}
	}
?>